package com.example.xlmkotlinwanandroid.ui.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.xlmkotlinwanandroid.R
import com.example.xlmkotlinwanandroid.base.BaseFragment
import com.example.xlmkotlinwanandroid.base.BaseVMFragment
import com.example.xlmkotlinwanandroid.databinding.FragmentHomeBinding
import com.example.xlmkotlinwanandroid.databinding.FragmentTabBinding
import com.example.xlmkotlinwanandroid.ext.dp2Px
import com.example.xlmkotlinwanandroid.ext.observer
import com.example.xlmkotlinwanandroid.model.bean.ArticleBean
import com.example.xlmkotlinwanandroid.model.viewmodel.ProjectViewModel
import com.example.xlmkotlinwanandroid.network.ArticleViewModel2
import com.example.xlmkotlinwanandroid.ui.adapter.ArticleAdapter
import com.example.xlmkotlinwanandroid.ui.adapter.ProjectAdapter
import com.example.xlmkotlinwanandroid.view.SpaceItemDecoration

class TabFragment(val cid: Int) : BaseVMFragment<ProjectViewModel>() {

    private lateinit var tabBinding: FragmentTabBinding

    private val articleAdapter by lazy { ProjectAdapter(activity) }


    override fun viewBinding(inflater: LayoutInflater, container: ViewGroup?): View {
        tabBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_tab, container, false);
        return tabBinding.root
    }

    override fun initViews() {
        setUpArticle()
    }

    override fun initData() {
    }

    override fun observeViewModel() {
        observer(ProjectViewModel().getProjectArticleByType(true, cid), ::updateArticle)
    }

    override fun viewModelClass(): Class<ProjectViewModel> {
        return ProjectViewModel::class.java
    }

    //设置
    private fun setUpArticle() {
        //recyclerView初始化

        tabBinding.recyclerView.layoutManager =
            LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)

        tabBinding.recyclerView.addItemDecoration(SpaceItemDecoration(0, 8f.dp2Px(), false))
        tabBinding.recyclerView.adapter = articleAdapter
        //下拉刷新
        tabBinding.swipe.setOnRefreshListener {
            observer(ProjectViewModel().getProjectArticleByType(true, cid), ::updateArticle)
            tabBinding.swipe.isRefreshing = false
        }
        //上拉加载更多
        tabBinding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
            }

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                val manager = recyclerView.layoutManager //获取LayoutManager
                if (manager != null && manager is LinearLayoutManager) {
                    //第一个可见的位置
                    val firstPosition = manager.findFirstVisibleItemPosition()
                    val lastPosition = manager.findLastCompletelyVisibleItemPosition()
                    //如果 dx>0 则表示 右滑 ,dx<0 表示 左滑,dy <0 表示 上滑, dy>0 表示下滑
                    if(manager.childCount>0&&lastPosition>manager.itemCount-2){
                        observer(ProjectViewModel().getProjectArticleByType(false, cid), ::updateArticle)
                    }
                }
            }

        })


    }

    private fun updateArticle(it: ArrayList<ArticleBean>) {
        articleAdapter.setData(it)
        articleAdapter.notifyDataSetChanged()
    }
}